//
//  DataUploadService.m
//  Guazi
//
//  Created by koolearn on 11-6-27.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "DataUploadService.h"
#import "OperationLog.h"
#import "GuaziAppDelegate.h"

static DataUploadService* dataupServiceInstance;

@implementation DataUploadService


- (id) init 
{
    self = [super init];
    if ( self ) {
        operationQueue = [[NSOperationQueue alloc] init];
        [operationQueue setMaxConcurrentOperationCount:1];
        lastTime = [[NSDate date] timeIntervalSince1970];
        runing = NO;
    }
    return self;
}


- (void) dealloc
{
    [operationQueue release];
    [super dealloc];
}


+ (id) sharedInstance
{
    if ( dataupServiceInstance == nil ) {
        dataupServiceInstance = [[DataUploadService alloc] init];
    }
    return dataupServiceInstance;
}


- (void) run
{
    [self run:300];
}


- (void) run:(NSInteger)seconds
{
    User* user = [GuaziAppDelegate getAppDelegate].user;
    if ( [kUserStateLocal compare:user.state] == NSOrderedSame ) return;
    
    //5分钟后上传
    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
    if ( now - lastTime < seconds ) return;
    lastTime = now;
    
    NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(upload) object:nil];
    [operationQueue addOperation:operation];
    [operation release];
}


- (NSArray*) selectData
{
    return [OperationLog selectForUpload:3 offset:0 limit:200]; 
}


- (void) upload
{
    NSArray* records = [self selectData];
    if ( [records count] == 0 ) return;
    
    //client = [[TwitterClient alloc] initWithTarget:self action:@selector(didUpload:obj:)];
    //[client dataup:records];
    
    NSHTTPURLResponse* response;
	NSError* error;
	
    id object = [TwitterClient dataupSync:records response:&response error:&error];
    [self didUpload:object];
    
}


- (void) didUpload:(id)obj
{
    if ( obj == nil ) return;
    if ( ![obj isKindOfClass:[NSArray class]] ) return;
    
    NSArray* arr = (NSArray*)obj;
    
    long long logId;
    BOOL result;
    NSString* module;
    NSString* operation;
    
    for ( id o in arr ) {
        if ( o == nil ) continue;
        if ( ![o isKindOfClass:[NSDictionary class]] ) continue;
        
        NSDictionary* dic = (NSDictionary*)o;
        logId = [[dic objectForKey:@"operationId"] longLongValue];
        
        id i = [dic objectForKey:@"dataUPIsSucc"];
        if ( i == [NSNull null] ) {
            result = NO;
        }
        else {
            if ( [i compare:@"Y" options:NSCaseInsensitiveSearch] == NSOrderedSame ) {
                result = YES;
            }
            else {
                result = NO;
            }
        }

        [OperationLog updateResult:result logId:logId];
        
        i = [dic objectForKey:@"returnParams"];
        if ( i && i!=[NSNull null] && [i isKindOfClass:[NSDictionary class]] ) {
            NSDictionary* paramsDic = (NSDictionary*) i;
            module = [paramsDic objectForKey:@"module"];
            operation = [paramsDic objectForKey:@"operation"];

            if ( [kOperationLogModulePerson compare:module] == NSOrderedSame ) {
                if ( [kOperationLogOperationAdd compare:operation] == NSOrderedSame ) {
                    //增加联系人。userId可以有变化
                }
            }
        }
        
    }
}


@end
